package csp;

import java.util.List;
import java.util.Map;

public class QueenConstraint implements Constraint<String,String> {
	
	private String value1;

	private String value2;
	
	//二元约束
	public QueenConstraint(String value1,String value2) {
		this.value1 = value1;
		this.value2 = value2;
	}
	
	@Override
	public boolean satisfied(Map<String,String> assignment) {
		String getValue1 = assignment.get(value1);
		String getValue2 = assignment.get(value2);
		if(getValue1==null||getValue2==null) {
			return true;
		}
		if(getValue1.equals(getValue2)) {
			return false;
		}
		if ((getValue1.split(",")[0].equals(getValue2.split(",")[0]))||(getValue1.split(",")[1].equals(getValue2.split(",")[1]))) {
			return false;
		}
		int x = Math.abs(Integer.parseInt(getValue1.split(",")[0])-Integer.parseInt(getValue2.split(",")[0]));
		int y = Math.abs(Integer.parseInt(getValue1.split(",")[1])-Integer.parseInt(getValue2.split(",")[1]));
		if(x==y) {
			return false;
		}else {
			return true;
		}
	}
	
	@Override
	public List<String> getVariables() {
		return List.of(value1,value2);
	}

}